package com.wondream.jenkins.utils;

import com.alibaba.fastjson.JSON;
import com.wondream.jenkins.service.JenkinsAutomation;
import com.wondream.jenkins.JenkinsServer;
import com.wondream.jenkins.helper.Range;
import com.wondream.jenkins.model.*;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangjun
 * @note JenkinsUtils
 * @date 2025-01-02  9:28
 */
public class JenkinsUtils {

    public static JenkinsServer getJenkinsServer(){
        String jenkinsUrl = EnvironmentUtils.getProperty("app.jenkins.url", "http://platform:1128628f941f3aa80fb9cc2a8e28402028@192.168.0.244:8080");
        String jenkinsUsername = EnvironmentUtils.getProperty("app.jenkins.username", "platform");
        String jenkinsPassword = EnvironmentUtils.getProperty("app.jenkins.password", "1128628f941f3aa80fb9cc2a8e28402028");
//        String jenkinsUrl = System.getProperty("app.jenkins.url", "http://gzgadev:11ce1003529d65074c62728804e61ff118@192.168.0.158:7506");
//        String jenkinsUsername = System.getProperty("app.jenkins.username", "gzgadev");
//        String jenkinsPassword = System.getProperty("app.jenkins.password", "11ce1003529d65074c62728804e61ff118");

        try {
            return new JenkinsServer(new URI(jenkinsUrl), jenkinsUsername, jenkinsPassword);
        } catch (URISyntaxException e) {

        }
        return null;
    }

    public static void buildJob1(String jobName) {
        try {
            JenkinsServer jenkins = getJenkinsServer();
            JobWithDetails job = jenkins.getJob(jobName);
            Map<String, String> params = new HashMap<>();
            params.put("myBuildType", "all");
//            QueueReference reference = job.build(params);
            QueueReference reference = job.build();
            System.out.println("Build triggered for job: " + jobName);
            System.out.println(JSON.toJSONString(reference.getQueueItemUrlPart()));

//            Queue queue = jenkins.getQueue();
//            QueueItem queueItem = jenkins.getQueueItem(reference);
//            Build build = jenkins.getBuild(job.getQueueItem());
            Range range = Range.build().from(1).to(5);
            List<Build> allBuilds = job.getAllBuilds(range);

            JenkinsAutomation.monitorJob(jenkins, job, jobName, reference);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void buildJob(String jobName) {
        try {
            JenkinsServer jenkins = getJenkinsServer();
            JobWithDetails job = jenkins.getJob(jobName);
            Map<String, String> params = new HashMap<>();
            params.put("myBuildType", "all");
//            QueueReference reference = job.build(params);
            QueueReference reference = job.build(true);
            System.out.println("Build triggered for job: " + jobName);
            System.out.println(JSON.toJSONString(reference.getQueueItemUrlPart()));

//            Queue queue = jenkins.getQueue();
//            QueueItem queueItem = jenkins.getQueueItem(reference);
//            Build build = jenkins.getBuild(job.getQueueItem());
//            Range range = Range.build().from(1).to(5);
//            List<Build> allBuilds = job.getAllBuilds(range);
//            Optional<Build> build = job.getBuildByNumber(164);
//            if (build.isPresent()) {
//                Build build1 = build.get();
//            }

//            JenkinsAutomation.monitorJob(jenkins, job, jobName, reference);
            queryJobByQueueUrl(jobName, reference.getQueueItemUrlPart());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static BuildResult queryJobByQueueUrl(String jobName, String url) {
        try {
            JenkinsServer jenkins = getJenkinsServer();
//            JobWithDetails job = jenkins.getJob(jobName);

            QueueReference reference = new QueueReference(url);
            System.out.println(JSON.toJSONString(reference.getQueueItemUrlPart()));

            return JenkinsAutomation.queryJobResult(jenkins, jobName, reference);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BuildResult.UNKNOWN;
    }

    public static BuildResult queryJobByBuildUrl(String jobName, String buildUrl) {
        try {
            JenkinsServer jenkins = getJenkinsServer();
//            JobWithDetails job = jenkins.getJob(jobName);
            return JenkinsAutomation.queryJobResult(jenkins, jobName, buildUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BuildResult.UNKNOWN;
    }
}
